# -*-coding:utf8-*-

import numpy as np
import cv2
import picamera
import os
from gpiozero import LED
from time import sleep

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = picamera.PiCamera()

def catchpic(add):
	cam.capture(add)
	return 0

def facedetection(add):
	img = cv2.imread(add)
	gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

	faces=face_cascade.detectMultiScale(
		gray,
		scaleFactor = 1.15,
		minNeighbors = 4,
		minSize = (5,5),
		flags = cv2.CASCADE_SCALE_IMAGE,
	)

	if len(faces)>0:
		return 1;
	return 0;


if __name__ == '__main__':

	pic_add = 'imageTmp.jpg'

	led = LED(17)

	while 1:
		catchpic(pic_add)
		if facedetection(pic_add) == 1:
			led.on()
			print "Find Face!"
		else:
			led.off()
			print "X"
		os.remove(pic_add)
		
	os.remove(pic_add)


	
